<?php

/**
 * 文件上传类
 * @author bing.peng
 *
 */
class Upload {
	
	var $data = array();
	
	function do_upload($field, $dir) {
		if( ! isset($_FILES[$field]) ) {
			return flase;
		}
		
		$this->data['file_name'] = $_FILES[$field]['name'];
		$extend = pathinfo( $this->data['file_name'] ); 
		$this->data['extend'] = strtolower($extend["extension"]);
		
		$this->data['new_file_name'] = rand(1,100000) . "." . $this->data['extend'];
		
		
		$target_path = ABSPATH . $dir . '/';
		$this->mkdirs( $target_path );
		$target_path .= $this->data['new_file_name'];
		
		$this->data['file_dir'] = $dir . '/';
		$this->data['file_path'] = $this->data['file_dir'] . $this->data['new_file_name'];
		
		
   		if(@move_uploaded_file($_FILES[$field]['tmp_name'], $target_path)) {
   			return $this->data;
   		}
   		else {
   			echo "upload error";
   			exit;
   		}
	}
	
	function mkdirs($dir) {
		if( ! is_dir( $dir ) )	{  
			if( ! $this->mkdirs( dirname($dir) ) ) {  
				return false;  
			}  
			if( ! mkdir($dir,0777) ) {  
				return false;  
			}  
		}  
		return true;
	}
}

?>